package com.gitee.liuzhi.demo.mapstruct.utils;

import org.apache.commons.collections4.CollectionUtils;
import org.mapstruct.Named;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * Created by 2022/4/21/0021
 *
 * @author LiuZhi
 * @version V1.0
 * @description: 转换工具类 ConverterUtils
 */
public class ConverterUtils {

    /**
     * 将单值转换为单集合
     *
     * @param t   t
     * @param <T> T
     * @return List<T>
     */
    @Named("singleToList")
    public static <T> List<T> singleToList(T t) {
        if (Objects.isNull(t)) {
            return null;
        }
        return Collections.singletonList(t);
    }

    /**
     * 将集合转换为单值
     *
     * @param list list
     * @param <T>  T
     * @return t
     */
    public static <T> T listToSingle(List<T> list) {
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.stream().findFirst().orElse(null);
    }

}
